<html>
 <head>
  <meta charset="utf-8"/>
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport"/>
  <title>
   Flask 框架作者希望看到的 Python  | 数螺 | NAUT IDEA
  </title>
  <link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" rel="stylesheet"/>
  <link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
  <style type="text/css">
   #xmain img {
                  max-width: 100%;
                  display: block;
                  margin-top: 10px;
                  margin-bottom: 10px;
                }

                #xmain p {
                    line-height:150%;
                    font-size: 16px;
                    margin-top: 20px;
                }

                #xmain h2 {
                    font-size: 24px;
                }

                #xmain h3 {
                    font-size: 20px;
                }

                #xmain h4 {
                    font-size: 18px;
                }


                .header {
	           background-color: #0099ff;
	           color: #ffffff;
	           margin-bottom: 20px;
	        }

	        .header p {
                  margin: 0px;
                  padding: 10px 0;
                  display: inline-block;  
                  vertical-align: middle;
                  font-size: 16px;
               }

               .header a {
                 color: white;
               }

              .header img {
                 height: 25px;
              }
  </style>
  <script src="http://cdn.bootcss.com/jquery/3.0.0/jquery.min.js">
  </script>
  <script src="http://nautstatic-10007657.file.myqcloud.com/static/css/readability.min.js" type="text/javascript">
  </script>
  <script type="text/javascript">
   $(document).ready(function() {
                 var loc = document.location;
                 var uri = {
                  spec: "http://dataunion.org/20024.html",
                  host: "http://dataunion.org",
                  prePath: "http://dataunion.org",
                  scheme: "http",
                  pathBase: "http://dataunion.org/"
                 };
    
                 var documentClone = document.cloneNode(true);
                 var article = new Readability(uri, documentClone).parse();
     
                 document.getElementById("xmain").innerHTML = article.content;
                });
  </script>
  <!-- 1466461225: Accept with keywords: (title(0.2):Python,社区,作者,数盟,框架, topn(0.266666666667):社区,语言,数盟,特性,程序员,数据挖掘,联系,状态,自定义,Python,东西,对象,参数,开发者,内建,类型,方法,字典,解释器,函数,python,文章,比如说,行业资讯,数据,代码,系统,旧式,时候,全局).-->
 </head>
 <body onload="">
  <div class="header">
   <div class="container">
    <div class="row">
     <div class="col-xs-6 col-sm-6 text-left">
      <a href="/databee">
       <img src="http://nautidea-10007657.cos.myqcloud.com/logo_white.png"/>
      </a>
      <a href="/databee">
       <p>
        数螺
       </p>
      </a>
     </div>
     <div class="hidden-xs col-sm-6 text-right">
      <p>
       致力于数据科学的推广和知识传播
      </p>
     </div>
    </div>
   </div>
  </div>
  <div class="container text-center">
   <h1>
    Flask 框架作者希望看到的 Python
   </h1>
  </div>
  <div class="container" id="xmain">
   ﻿﻿
   <title>
    Flask 框架作者希望看到的 Python | 数盟社区
   </title>
   <!-- All in One SEO Pack 2.2.7.6.2 by Michael Torbert of Semper Fi Web Design[32,69] -->
   <!-- /all in one seo pack -->
   <!--
<div align="center">
<a href="http://strata.oreilly.com.cn/hadoop-big-data-cn?cmp=mp-data-confreg-home-stcn16_dataunion_pc" target="_blank"><img src="http://dataunion.org/wp-content/uploads/2016/05/stratabj.jpg"/ ></a>
</div>
-->
   <header id="header-web">
    <div class="header-main">
     <hgroup class="logo">
      <h1>
       <a href="http://dataunion.org/" rel="home" title="数盟社区">
        <img src="http://dataunion.org/wp-content/themes/yzipi/images/logo.png"/>
       </a>
      </h1>
     </hgroup>
     <!--logo-->
     <nav class="header-nav">
      <ul class="menu" id="menu-%e4%b8%bb%e8%8f%9c%e5%8d%95">
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-71" id="menu-item-71">
        <a href="http://dataunion.org/category/events" title="events">
         活动
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-22457" id="menu-item-22457">
          <a href="http://dataunion.org/2016timeline">
           2016档期
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-22459" id="menu-item-22459">
          <a href="http://dataunion.org/category/parterc">
           合作会议
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category current-post-ancestor current-menu-parent current-post-parent menu-item-has-children menu-item-20869" id="menu-item-20869">
        <a href="http://dataunion.org/category/tech" title="articles">
         文章
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20867" id="menu-item-20867">
          <a href="http://dataunion.org/category/tech/base" title="base">
           基础架构
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3302" id="menu-item-3302">
          <a href="http://dataunion.org/category/tech/ai" title="ai">
           人工智能
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3303" id="menu-item-3303">
          <a href="http://dataunion.org/category/tech/analysis" title="analysis">
           数据分析
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21920" id="menu-item-21920">
          <a href="http://dataunion.org/category/tech/dm">
           数据挖掘
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3314" id="menu-item-3314">
          <a href="http://dataunion.org/category/tech/viz" title="viz">
           可视化
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3305" id="menu-item-3305">
          <a href="http://dataunion.org/category/tech/devl" title="devl">
           编程语言
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-20876" id="menu-item-20876">
        <a href="http://dataunion.org/category/industry">
         行业
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-16328" id="menu-item-16328">
          <a href="http://dataunion.org/category/industry/case" title="case">
           行业应用
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-2112" id="menu-item-2112">
          <a href="http://dataunion.org/category/industry/demo" title="demo">
           Demo展示
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21562" id="menu-item-21562">
          <a href="http://dataunion.org/category/industry/news">
           行业资讯
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-311" id="menu-item-311">
        <a href="http://dataunion.org/category/sources" title="sources">
         资源
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20870" id="menu-item-20870">
        <a href="http://dataunion.org/category/books" title="book">
         图书
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21363" id="menu-item-21363">
        <a href="http://dataunion.org/category/training">
         课程
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-21853" id="menu-item-21853">
        <a href="http://dataunion.org/category/jobs">
         职位
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-22050" id="menu-item-22050">
          <a href="http://dataunion.org/category/career">
           职业规划
          </a>
         </li>
        </ul>
       </li>
      </ul>
     </nav>
     <!--header-nav-->
    </div>
   </header>
   <!--header-web-->
   <div id="main">
    <div id="soutab">
     <form action="http://dataunion.org/" class="search" method="get">
     </form>
    </div>
    <div id="container">
     <nav id="mbx">
      当前位置：
      <a href="http://dataunion.org">
       首页
      </a>
      &gt;
      <a href="http://dataunion.org/category/tech">
       文章
      </a>
      &gt;  正文
     </nav>
     <!--mbx-->
     <article class="content">
      <header align="centre" class="contenttitle">
       <div class="mscc">
        <h1 class="mscctitle">
         <a href="http://dataunion.org/20024.html">
          Flask 框架作者希望看到的 Python
         </a>
        </h1>
        <address class="msccaddress ">
         <em>
          1,839 次阅读 -
         </em>
         <a href="http://dataunion.org/category/tech" rel="category tag">
          文章
         </a>
        </address>
       </div>
      </header>
      <div class="content-text">
       <p>
        出处：
        <a href="http://python.jobbole.com/76569/">
         伯乐在线
        </a>
       </p>
       <blockquote>
        <p>
         本文作者 Armin Ronacher 是知名 Python 框架
         <a href="http://hao.jobbole.com/flask/" target="_blank">
          Flask
         </a>
         的开发者。文章写于 2014 年 8 月。
        </p>
       </blockquote>
       <p>
        我不是Python 3的粉，也不喜欢这门语言的发展方向，这都不是什么秘密了。这也导致了最近几个月，铺天盖地邮件询问我，我到底想要Python怎样。所以我觉得我应该公开分享一下我的想法，给将来的程序语言设计者提供一些灵感。：）
       </p>
       <p>
        显然Python作为一门编程程序，并不完美。然而，让我感到沮丧的是，这门语言大多数问题都与解释器的细节有关，很少是语言本身。然而这些解释器的细节，正在变成语言的一部分，这也是为什么这一点是很重要的。
       </p>
       <p>
        我希望能带你们一起遨游，从解释器中古怪的槽(slots)开始，到python语言设计中最大的错误结束。如果反响比较好的话，以后还会有更多类似的文章。
       </p>
       <p>
        大体上讲，这些文章将会探索解释器中的设计决策以及这些决策是如何影响解释器和语言的。我相信从程序语言设计的角度来看本文，会比将其作为对python将来如何发展的建议会更有趣。
       </p>
       <p>
       </p>
       <h3>
        语言vs实现
       </h3>
       <p>
        完成本文的初版后，我又添加了这段文字，因为我觉的我们在很大程度上忽视了，Python作为一种语言，CPython作为一种解释器，他们之间并没有开发者认为的那样分得开。
       </p>
       <p>
        <strong>
         虽说有语言规范，但是大多数情况下，它只是规定了解释器要做什么，甚至连这些都没有规定。
        </strong>
       </p>
       <p>
        在这种特定的情况下，解释器的这种晦涩的实现细节，改变或是影响了语言的设计，并且迫使Python的其他实现方式要去适应它。比如我猜想PyPy并不知道什么是槽(slots)，但是仍然要操作它，就好像槽(slots)是解释器的一部分一样。
       </p>
       <p>
       </p>
       <h3>
        槽(Slots)
       </h3>
       <p>
        目前为止，我对专门语言最大的意见就是愚蠢的槽(slots)系统。
        <strong>
         我指的不是
         <code>
          __slot__
         </code>
         ，而是特殊方法的内部类型槽
        </strong>
        。这些槽(slots)是语言的“特性”，这点在很大程度上是错误的，因为你很少需要去关心它。也就是说，我认为，槽(slots)存在的这一事实，是这门语言最大的问题。
       </p>
       <p>
        那么槽(slots)是什么东西呢？slot是解释器内部实现时产生的副作用。每个Python程序员都知道所谓的“魔术方法”，比如
        <code>
         __add__
        </code>
        。这些方法以两个下划线开头，后面是这种特殊方法的名称，然后又是两个下划线。正如每位开发者所知，a+b就相当于
        <code>
         a.__add__(b)
        </code>
       </p>
       <p>
        不幸的是，这是一个谎言
       </p>
       <p>
        Python实际上并不是这样工作的。现如今的Python内部完全不是这样工作的。这里我们大致讲解解释器是如何工作的：
       </p>
       <ol>
        <li>
         当一个类型被创建后，解释器会寻找类里面所有的描述符，同时还会寻找类似
         <code>
          __add__
         </code>
         这样的特殊方法。
        </li>
        <li>
         对于每一个特殊方法，解释器找到它，把描述符的引用放在类型对象的一个预定义的槽(slots)中。
        </li>
       </ol>
       <p>
        例如，特殊方法
        <strong>
         add
        </strong>
        对应两个内部槽(slots)，tp_as_number-&gt;nb_add 和 tp_as_sequence-&gt;sq_concat.
       </p>
       <ol>
        <li>
         当解释器想求a+b的值时，它会进行类似于TYPE_OF(a)-&gt;tp_as_number-&gt;nb_add(a, b)的调用（实际上比这复杂的多，因为
         <code>
          __add__
         </code>
         实际上有多个槽）
        </li>
       </ol>
       <p>
        因此，表面上看
        <code>
         a+b
        </code>
        类似于
        <code>
         type(a).__add__(a, b)
        </code>
        ，但是即使这样还是不准确的，这点你可以从槽(slots)的处理中看出来。你自己就可以轻易的证明这件事，通过在元类里实现
        <br/>
        <code>
         __getattribute__
        </code>
        并尝试连接一个自定义的
        <code>
         __add__
        </code>
        。你会发现，它永远不会被调用。
       </p>
       <p>
        我认为槽(slots)系统简直荒唐透顶。对于一些特定的类型(比如整型)，它是一种优化，但是对于其他的类型则是毫无意义的。
       </p>
       <p>
        为了证明这一点，请看这种完全无意义的类型(
        <code>
         x.py
        </code>
        )：
       </p>
       <div class="crayon-syntax crayon-theme-github crayon-font-monaco crayon-os-mac print-yes notranslate" data-settings=" minimize scroll-always" id="crayon-55a64ab84c906074047477">
        <div class="crayon-toolbar" data-settings=" show">
         <div class="crayon-tools">
          <div class="crayon-button crayon-nums-button crayon-pressed" title="切换是否显示行编号">
          </div>
          <p>
           <span class="crayon-language">
            Python
           </span>
          </p>
         </div>
        </div>
        <div class="crayon-plain-wrap">
        </div>
        <div class="crayon-main">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
            </td>
            <td class="crayon-code">
             <div class="crayon-pre">
              <div class="crayon-line" id="crayon-55a64ab84c906074047477-1">
               <span class="crayon-sy">
                ;
               </span>
               <span class="crayon-v">
                html
               </span>
               <span class="crayon-o">
                –
               </span>
               <span class="crayon-v">
                script
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-t">
                false
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-e">
                A
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-k ">
                object
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c906074047477-2">
               <span class="crayon-h">
               </span>
               <span class="crayon-r">
                def
               </span>
               <span class="crayon-e">
                __add__
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-r">
                self
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-v">
                other
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c906074047477-3">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                return
               </span>
               <span class="crayon-cn">
                42
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        因为我们有一个
        <code>
         __add__
        </code>
        方法，解释器会在槽(slots)中建立它。速度有多快呢？我们做
        <strong>
         a+b
        </strong>
        的时候，会用到槽(slots)，下面是执行所用时间：
       </p>
       <div class="crayon-syntax crayon-theme-github crayon-font-monaco crayon-os-mac print-yes notranslate" data-settings=" minimize scroll-always" id="crayon-55a64ab84c914141863399">
        <div class="crayon-toolbar" data-settings=" show">
         <div class="crayon-tools">
          <div class="crayon-button crayon-nums-button crayon-pressed" title="切换是否显示行编号">
          </div>
          <p>
           <span class="crayon-language">
            Python
           </span>
          </p>
         </div>
        </div>
        <div class="crayon-plain-wrap">
        </div>
        <div class="crayon-main">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
            </td>
            <td class="crayon-code">
             <div class="crayon-pre">
              <div class="crayon-line" id="crayon-55a64ab84c914141863399-1">
               <span class="crayon-sy">
                ;
               </span>
               <span class="crayon-v">
                html
               </span>
               <span class="crayon-o">
                –
               </span>
               <span class="crayon-v">
                script
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-t">
                false
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                $
               </span>
               <span class="crayon-v">
                python3
               </span>
               <span class="crayon-o">
                –
               </span>
               <span class="crayon-v">
                mtimeit
               </span>
               <span class="crayon-o">
                –
               </span>
               <span class="crayon-i">
                s
               </span>
               <span class="crayon-s">
                ‘from x import A; a = A(); b = A()’
               </span>
               <span class="crayon-s">
                ‘a + b’
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c914141863399-2">
               <span class="crayon-cn">
                1000000
               </span>
               <span class="crayon-v">
                loops
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-e">
                best
               </span>
               <span class="crayon-i">
                of
               </span>
               <span class="crayon-cn">
                3
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-cn">
                0.256
               </span>
               <span class="crayon-e">
                usec
               </span>
               <span class="crayon-e">
                per
               </span>
               <span class="crayon-v">
                loop
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        然而如果我们使用
        <code>
         a.__add__(b)
        </code>
        ，则会绕过槽(slots)系统。解释器会去查看实例字典(在那里它什么也找不到)，然后取查看类型字典，它将找到这个方法。下面是执行所需的时间
       </p>
       <div class="crayon-syntax crayon-theme-github crayon-font-monaco crayon-os-mac print-yes notranslate" data-settings=" minimize scroll-always" id="crayon-55a64ab84c91a740639409">
        <div class="crayon-toolbar" data-settings=" show">
         <div class="crayon-tools">
          <div class="crayon-button crayon-nums-button crayon-pressed" title="切换是否显示行编号">
          </div>
          <p>
           <span class="crayon-language">
            Python
           </span>
          </p>
         </div>
        </div>
        <div class="crayon-plain-wrap">
        </div>
        <div class="crayon-main">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
            </td>
            <td class="crayon-code">
             <div class="crayon-pre">
              <div class="crayon-line" id="crayon-55a64ab84c91a740639409-1">
               <span class="crayon-sy">
                ;
               </span>
               <span class="crayon-v">
                html
               </span>
               <span class="crayon-o">
                –
               </span>
               <span class="crayon-v">
                script
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-t">
                false
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                $
               </span>
               <span class="crayon-v">
                python3
               </span>
               <span class="crayon-o">
                –
               </span>
               <span class="crayon-v">
                mtimeit
               </span>
               <span class="crayon-o">
                –
               </span>
               <span class="crayon-i">
                s
               </span>
               <span class="crayon-s">
                ‘from x import A; a = A(); b = A()’
               </span>
               <span class="crayon-s">
                ‘a.__add__(b)’
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c91a740639409-2">
               <span class="crayon-cn">
                10000000
               </span>
               <span class="crayon-v">
                loops
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-e">
                best
               </span>
               <span class="crayon-i">
                of
               </span>
               <span class="crayon-cn">
                3
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-cn">
                0.158
               </span>
               <span class="crayon-e">
                usec
               </span>
               <span class="crayon-e">
                per
               </span>
               <span class="crayon-v">
                loop
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        你敢信吗：不使用槽(slots)的版本实际上更快。这是什么魔力？这一现象的原因我不是很明白，但是这种情况已经持续了很久很久了。实际上，对运算符来讲旧式的类(旧式类不含有槽(slots))比新式的类速度要快很多，并且有更多的特性。
       </p>
       <p>
        更多的特性？没错，因为旧式的类可以这样做(Python 2.7):
       </p>
       <div class="crayon-syntax crayon-theme-github crayon-font-monaco crayon-os-mac print-yes notranslate" data-settings=" minimize scroll-always" id="crayon-55a64ab84c920244690439">
        <div class="crayon-toolbar" data-settings=" show">
         <div class="crayon-tools">
          <div class="crayon-button crayon-nums-button crayon-pressed" title="切换是否显示行编号">
          </div>
          <p>
           <span class="crayon-language">
            Python
           </span>
          </p>
         </div>
        </div>
        <div class="crayon-plain-wrap">
        </div>
        <div class="crayon-main">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
            </td>
            <td class="crayon-code">
             <div class="crayon-pre">
              <div class="crayon-line" id="crayon-55a64ab84c920244690439-1">
               <span class="crayon-sy">
                ;
               </span>
               <span class="crayon-v">
                html
               </span>
               <span class="crayon-o">
                –
               </span>
               <span class="crayon-v">
                script
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-t">
                false
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-o">
                &gt;&gt;&gt;
               </span>
               <span class="crayon-v">
                original
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-cn">
                42
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c920244690439-2">
               <span class="crayon-o">
                &gt;&gt;&gt;
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-v">
                FooProxy
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c920244690439-3">
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-r">
                def
               </span>
               <span class="crayon-e">
                __getattr__
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-r">
                self
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-v">
                x
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c920244690439-4">
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                return
               </span>
               <span class="crayon-k ">
                getattr
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                original
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-v">
                x
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c920244690439-5">
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-sy">
                .
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c920244690439-6">
               <span class="crayon-o">
                &gt;&gt;&gt;
               </span>
               <span class="crayon-v">
                proxy
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-e">
                FooProxy
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c920244690439-7">
               <span class="crayon-o">
                &gt;&gt;&gt;
               </span>
               <span class="crayon-i">
                proxy
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c920244690439-8">
               <span class="crayon-cn">
                42
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c920244690439-9">
               <span class="crayon-o">
                &gt;&gt;&gt;
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-o">
                +
               </span>
               <span class="crayon-i">
                proxy
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c920244690439-10">
               <span class="crayon-cn">
                43
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c920244690439-11">
               <span class="crayon-o">
                &gt;&gt;&gt;
               </span>
               <span class="crayon-v">
                proxy
               </span>
               <span class="crayon-o">
                +
               </span>
               <span class="crayon-cn">
                1
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c920244690439-12">
               <span class="crayon-cn">
                43
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        是的。对于一个复杂的类型系统而言，如今我们拥有的特性比Python2还少。因为上面的那段代码，是不能新式类中使用的。
       </p>
       <div class="crayon-syntax crayon-theme-github crayon-font-monaco crayon-os-mac print-yes notranslate" data-settings=" minimize scroll-always" id="crayon-55a64ab84c925314881871">
        <div class="crayon-toolbar" data-settings=" show">
         <div class="crayon-tools">
          <div class="crayon-button crayon-nums-button crayon-pressed" title="切换是否显示行编号">
          </div>
          <p>
           <span class="crayon-language">
            Python
           </span>
          </p>
         </div>
        </div>
        <div class="crayon-plain-wrap">
        </div>
        <div class="crayon-main">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
            </td>
            <td class="crayon-code">
             <div class="crayon-pre">
              <div class="crayon-line" id="crayon-55a64ab84c925314881871-1">
               <span class="crayon-sy">
                ;
               </span>
               <span class="crayon-v">
                html
               </span>
               <span class="crayon-o">
                –
               </span>
               <span class="crayon-v">
                script
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-t">
                false
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-o">
                &gt;&gt;&gt;
               </span>
               <span class="crayon-r">
                import
               </span>
               <span class="crayon-k ">
                sys
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c925314881871-2">
               <span class="crayon-o">
                &gt;&gt;&gt;
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-v">
                OldStyleClass
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c925314881871-3">
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-r">
                pass
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c925314881871-4">
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-sy">
                .
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c925314881871-5">
               <span class="crayon-o">
                &gt;&gt;&gt;
               </span>
               <span class="crayon-t">
                class
               </span>
               <span class="crayon-e">
                NewStyleClass
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-k ">
                object
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c925314881871-6">
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-r">
                pass
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c925314881871-7">
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-sy">
                .
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c925314881871-8">
               <span class="crayon-o">
                &gt;&gt;&gt;
               </span>
               <span class="crayon-k ">
                sys
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                getsizeof
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                OldStyleClass
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c925314881871-9">
               <span class="crayon-cn">
                104
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c925314881871-10">
               <span class="crayon-o">
                &gt;&gt;&gt;
               </span>
               <span class="crayon-k ">
                sys
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                getsizeof
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                NewStyleClass
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c925314881871-11">
               <span class="crayon-cn">
                904
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        如果考虑到旧式的类是如此的轻量级，情况实际上更坏。
       </p>
       <p>
       </p>
       <h3>
        槽(slots)是哪来的？
       </h3>
       <p>
        这就提出了一个问题：为什么会存在槽(slots)。据我所知，槽(slots)的存在仅仅是一个历史遗留问题。
        <strong>
         当Python的解释器最初发明之时，像字符串等内建类型，被实现为全局变量，并且静态分配了结构体，用来存放一个类型所需的全部的特殊方法
        </strong>
        。
       </p>
       <p>
        这都是在
        <code>
         __add__
        </code>
        出现之前。如果你看一下1990年的Python，你可以看到当时的对象是如何创建的。
       </p>
       <p>
        比如说，整数看上去是这样的：
       </p>
       <div class="crayon-syntax crayon-theme-github crayon-font-monaco crayon-os-mac print-yes notranslate" data-settings=" minimize scroll-always" id="crayon-55a64ab84c92c723658751">
        <div class="crayon-toolbar" data-settings=" show">
         <div class="crayon-tools">
          <div class="crayon-button crayon-nums-button crayon-pressed" title="切换是否显示行编号">
          </div>
          <p>
           <span class="crayon-language">
            Python
           </span>
          </p>
         </div>
        </div>
        <div class="crayon-plain-wrap">
        </div>
        <div class="crayon-main">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
            </td>
            <td class="crayon-code">
             <div class="crayon-pre">
              <div class="crayon-line" id="crayon-55a64ab84c92c723658751-1">
               <span class="crayon-sy">
                ;
               </span>
               <span class="crayon-v">
                html
               </span>
               <span class="crayon-o">
                –
               </span>
               <span class="crayon-v">
                script
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-t">
                false
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-e">
                static
               </span>
               <span class="crayon-e">
                number_methods
               </span>
               <span class="crayon-v">
                int_as_number
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-sy">
                {
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c92c723658751-2">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                intadd
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-v">
                tp_add
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-o">
                /
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c92c723658751-3">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                intsub
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-v">
                tp_subtract
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-o">
                /
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c92c723658751-4">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                intmul
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-v">
                tp_multiply
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-o">
                /
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c92c723658751-5">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                intdiv
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-v">
                tp_divide
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-o">
                /
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c92c723658751-6">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                intrem
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-v">
                tp_remainder
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-o">
                /
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c92c723658751-7">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                intpow
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-v">
                tp_power
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-o">
                /
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c92c723658751-8">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                intneg
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-v">
                tp_negate
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-o">
                /
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c92c723658751-9">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                intpos
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-v">
                tp_plus
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-o">
                /
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c92c723658751-10">
               <span class="crayon-sy">
                }
               </span>
               <span class="crayon-sy">
                ;
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <div class="crayon-syntax crayon-theme-github crayon-font-monaco crayon-os-mac print-yes notranslate" data-settings=" minimize scroll-always" id="crayon-55a64ab84c931068860849">
        <div class="crayon-toolbar" data-settings=" show">
         <div class="crayon-tools">
          <div class="crayon-button crayon-nums-button crayon-pressed" title="切换是否显示行编号">
          </div>
          <p>
           <span class="crayon-language">
            Python
           </span>
          </p>
         </div>
        </div>
        <div class="crayon-plain-wrap">
        </div>
        <div class="crayon-main">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
            </td>
            <td class="crayon-code">
             <div class="crayon-pre">
              <div class="crayon-line" id="crayon-55a64ab84c931068860849-1">
               <span class="crayon-sy">
                ;
               </span>
               <span class="crayon-v">
                html
               </span>
               <span class="crayon-o">
                –
               </span>
               <span class="crayon-v">
                script
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-t">
                false
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-e">
                typeobject
               </span>
               <span class="crayon-v">
                Inttype
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-sy">
                {
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c931068860849-2">
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                OB_HEAD_INIT
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-o">
                &amp;
               </span>
               <span class="crayon-v">
                Typetype
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c931068860849-3">
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                ,
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c931068860849-4">
               <span class="crayon-h">
               </span>
               <span class="crayon-s">
                “int”
               </span>
               <span class="crayon-sy">
                ,
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c931068860849-5">
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                sizeof
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                intobject
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                ,
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c931068860849-6">
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                ,
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c931068860849-7">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                free
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-v">
                tp_dealloc
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-o">
                /
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c931068860849-8">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                intprint
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-v">
                tp_print
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-o">
                /
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c931068860849-9">
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-v">
                tp_getattr
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-o">
                /
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c931068860849-10">
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-v">
                tp_setattr
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-o">
                /
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c931068860849-11">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                intcompare
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-v">
                tp_compare
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-o">
                /
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c931068860849-12">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                intrepr
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-v">
                tp_repr
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-o">
                /
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c931068860849-13">
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                &amp;
               </span>
               <span class="crayon-v">
                int_as_number
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-v">
                tp_as_number
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-o">
                /
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c931068860849-14">
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-v">
                tp_as_sequence
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-o">
                /
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c931068860849-15">
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-v">
                tp_as_mapping
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-o">
                /
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c931068860849-16">
               <span class="crayon-sy">
                }
               </span>
               <span class="crayon-sy">
                ;
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        如你所见，即使在曾经发布的第一版Python中，
        <code>
         tp_as_number
        </code>
        就存在了。不幸的是，好像某天代码仓库因为一些修改而崩溃了，所以在一些非常老的Python版本中，很多重要的东西都丢失了（比如解释器），所以我们需要稍微往将来看看，看看对象是如何被实现的。到1993年，这是解释器的加法指令回调的样子：
       </p>
       <div class="crayon-syntax crayon-theme-github crayon-font-monaco crayon-os-mac print-yes notranslate" data-settings=" minimize scroll-always" id="crayon-55a64ab84c937365147742">
        <div class="crayon-toolbar" data-settings=" show">
         <div class="crayon-tools">
          <div class="crayon-button crayon-nums-button crayon-pressed" title="切换是否显示行编号">
          </div>
          <p>
           <span class="crayon-language">
            Python
           </span>
          </p>
         </div>
        </div>
        <div class="crayon-plain-wrap">
        </div>
        <div class="crayon-main">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
            </td>
            <td class="crayon-code">
             <div class="crayon-pre">
              <div class="crayon-line" id="crayon-55a64ab84c937365147742-1">
               <span class="crayon-sy">
                ;
               </span>
               <span class="crayon-v">
                html
               </span>
               <span class="crayon-o">
                –
               </span>
               <span class="crayon-v">
                script
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-t">
                false
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-e">
                static
               </span>
               <span class="crayon-k ">
                object
               </span>
               <span class="crayon-o">
                *
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c937365147742-2">
               <span class="crayon-e">
                add
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                v
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-k ">
                w
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c937365147742-3">
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                object
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-v">
                v
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-k ">
                w
               </span>
               <span class="crayon-sy">
                ;
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c937365147742-4">
               <span class="crayon-sy">
                {
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c937365147742-5">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                if
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                v
               </span>
               <span class="crayon-o">
                -&gt;
               </span>
               <span class="crayon-v">
                ob_type
               </span>
               <span class="crayon-o">
                -&gt;
               </span>
               <span class="crayon-v">
                tp_as_sequence
               </span>
               <span class="crayon-o">
                !=
               </span>
               <span class="crayon-v">
                NULL
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c937365147742-6">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                return
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-v">
                v
               </span>
               <span class="crayon-o">
                -&gt;
               </span>
               <span class="crayon-v">
                ob_type
               </span>
               <span class="crayon-o">
                -&gt;
               </span>
               <span class="crayon-v">
                tp_as_sequence
               </span>
               <span class="crayon-o">
                -&gt;
               </span>
               <span class="crayon-v">
                sq_concat
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                v
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-k ">
                w
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                ;
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c937365147742-7">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                else
               </span>
               <span class="crayon-st">
                if
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                v
               </span>
               <span class="crayon-o">
                -&gt;
               </span>
               <span class="crayon-v">
                ob_type
               </span>
               <span class="crayon-o">
                -&gt;
               </span>
               <span class="crayon-v">
                tp_as_number
               </span>
               <span class="crayon-o">
                !=
               </span>
               <span class="crayon-v">
                NULL
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                {
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c937365147742-8">
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                object
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-v">
                x
               </span>
               <span class="crayon-sy">
                ;
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c937365147742-9">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                if
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-k ">
                coerce
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-o">
                &amp;
               </span>
               <span class="crayon-v">
                v
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-o">
                &amp;
               </span>
               <span class="crayon-k ">
                w
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-o">
                !=
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c937365147742-10">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                return
               </span>
               <span class="crayon-v">
                NULL
               </span>
               <span class="crayon-sy">
                ;
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c937365147742-11">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                x
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-v">
                v
               </span>
               <span class="crayon-o">
                -&gt;
               </span>
               <span class="crayon-v">
                ob_type
               </span>
               <span class="crayon-o">
                -&gt;
               </span>
               <span class="crayon-v">
                tp_as_number
               </span>
               <span class="crayon-o">
                -&gt;
               </span>
               <span class="crayon-v">
                nb_add
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                v
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-k ">
                w
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                ;
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c937365147742-12">
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                DECREF
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                v
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                ;
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c937365147742-13">
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                DECREF
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-k ">
                w
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                ;
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c937365147742-14">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                return
               </span>
               <span class="crayon-v">
                x
               </span>
               <span class="crayon-sy">
                ;
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c937365147742-15">
               <span class="crayon-h">
               </span>
               <span class="crayon-sy">
                }
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c937365147742-16">
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                err_setstr
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-k ">
                TypeError
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-s">
                “bad operand type(s) for +”
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                ;
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c937365147742-17">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                return
               </span>
               <span class="crayon-v">
                NULL
               </span>
               <span class="crayon-sy">
                ;
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c937365147742-18">
               <span class="crayon-sy">
                }
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        那么
        <code>
         __add__
        </code>
        等是何时实现的呢？就我看来，他们出现于1.1版。我想方设法搞到了Python 1.1，并耍了一些手段在OS X 10.9上对其进行了编译：
       </p>
       <div class="crayon-syntax crayon-theme-github crayon-font-monaco crayon-os-mac print-yes notranslate" data-settings=" minimize scroll-always" id="crayon-55a64ab84c93d694174938">
        <div class="crayon-toolbar" data-settings=" show">
         <div class="crayon-tools">
          <span class="crayon-language">
           Python
          </span>
         </div>
        </div>
        <div class="crayon-plain-wrap">
        </div>
        <div class="crayon-main">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
            </td>
            <td class="crayon-code">
             <div class="crayon-pre">
              <div class="crayon-line" id="crayon-55a64ab84c93d694174938-1">
               <span class="crayon-sy">
                ;
               </span>
               <span class="crayon-v">
                html
               </span>
               <span class="crayon-o">
                –
               </span>
               <span class="crayon-v">
                script
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-t">
                false
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                $
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-v">
                python
               </span>
               <span class="crayon-o">
                –
               </span>
               <span class="crayon-i">
                v
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c93d694174938-2">
               <span class="crayon-i">
                Python
               </span>
               <span class="crayon-cn">
                1.1
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-i">
                Aug
               </span>
               <span class="crayon-cn">
                16
               </span>
               <span class="crayon-cn">
                2014
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c93d694174938-3">
               <span class="crayon-r">
                Copyright
               </span>
               <span class="crayon-cn">
                1991
               </span>
               <span class="crayon-o">
                –
               </span>
               <span class="crayon-cn">
                1994
               </span>
               <span class="crayon-e">
                Stichting
               </span>
               <span class="crayon-e">
                Mathematisch
               </span>
               <span class="crayon-v">
                Centrum
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-v">
                Amsterdam
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        当然，它经常会崩溃而且不是所有功能都能运行，但是它能让你看到Python从前的样子。比如说，C和Python在类型实现的时候有着巨大的不同：
       </p>
       <div class="crayon-syntax crayon-theme-github crayon-font-monaco crayon-os-mac print-yes notranslate" data-settings=" minimize scroll-always" id="crayon-55a64ab84c942168025967">
        <div class="crayon-toolbar" data-settings=" show">
         <div class="crayon-tools">
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
          </div>
          <p>
           <span class="crayon-language">
            Python
           </span>
          </p>
         </div>
        </div>
        <div class="crayon-plain-wrap">
        </div>
        <div class="crayon-main">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
            </td>
            <td class="crayon-code">
             <div class="crayon-pre">
              <div class="crayon-line" id="crayon-55a64ab84c942168025967-1">
               <span class="crayon-sy">
                ;
               </span>
               <span class="crayon-v">
                html
               </span>
               <span class="crayon-o">
                –
               </span>
               <span class="crayon-v">
                script
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-t">
                false
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                $
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-e">
                python
               </span>
               <span class="crayon-k ">
                test
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                py
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c942168025967-2">
               <span class="crayon-k ">
                Traceback
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-e">
                innermost
               </span>
               <span class="crayon-v">
                last
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c942168025967-3">
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                File
               </span>
               <span class="crayon-s">
                “test.py”
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-i">
                line
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-sy">
                ?
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c942168025967-4">
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                print
               </span>
               <span class="crayon-k ">
                dir
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-o">
                +
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c942168025967-5">
               <span class="crayon-k ">
                TypeError
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-k ">
                dir
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-e">
                argument
               </span>
               <span class="crayon-e">
                must
               </span>
               <span class="crayon-e">
                have
               </span>
               <span class="crayon-e">
                __dict__
               </span>
               <span class="crayon-v">
                attribute
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        如你所见，没有对内建类型(比如整型)进行检查。实际上，当
        <strong>
         add
        </strong>
        可以用于自定义类的时候，它是自定义类的完整特性：
       </p>
       <div class="crayon-syntax crayon-theme-github crayon-font-monaco crayon-os-mac print-yes notranslate" data-settings=" minimize scroll-always" id="crayon-55a64ab84c948439397458">
        <div class="crayon-toolbar" data-settings=" show">
         <div class="crayon-tools">
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
          </div>
          <p>
           <span class="crayon-language">
            Python
           </span>
          </p>
         </div>
        </div>
        <div class="crayon-plain-wrap">
        </div>
        <div class="crayon-main">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
            </td>
            <td class="crayon-code">
             <div class="crayon-pre">
              <div class="crayon-line" id="crayon-55a64ab84c948439397458-1">
               <span class="crayon-sy">
                ;
               </span>
               <span class="crayon-v">
                html
               </span>
               <span class="crayon-o">
                –
               </span>
               <span class="crayon-v">
                script
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-t">
                false
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-o">
                &gt;&gt;&gt;
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                __add__
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-cn">
                2
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c948439397458-2">
               <span class="crayon-k ">
                Traceback
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-e">
                innermost
               </span>
               <span class="crayon-v">
                last
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c948439397458-3">
               <span class="crayon-h">
               </span>
               <span class="crayon-k ">
                File
               </span>
               <span class="crayon-s">
                “&lt;stdin&gt;”
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-i">
                line
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-sy">
                ?
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c948439397458-4">
               <span class="crayon-k ">
                TypeError
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-v">
                attribute
               </span>
               <span class="crayon-o">
                –
               </span>
               <span class="crayon-e">
                less
               </span>
               <span class="crayon-k ">
                object
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        所以，这一传统一种保留到今天的Python中。Python中，类型的整体设计一直没有改变，但是补丁却打了很多很多年。
       </p>
       <p>
       </p>
       <h3>
        现代PyObject
       </h3>
       <p>
        如今，很多人会认为，C解释器中Python对象的实现，和实际Python代码中的Python对象实现，它们的差别非常微小。
        <strong>
         在 Python 2.7 中，最大的不同应该是在 Python 中默认的
         <code>
          __repr__
         </code>
         会报告 Python 中的类型实现为
         <code>
          class，
         </code>
         而 C 中的类型实现为
         <code>
          type。
         </code>
        </strong>
        事实上，repr中的不同表示了一个类型是静态分配(类型)还是是在堆上动态分配。它并不会产生什么实际差异，而且在Python3中这些都已经不存在了。特殊方法被复制到槽，反之亦然。在很大程度上，Python和C类的差别貌似已经消失了。
       </p>
       <p>
        然而，不幸的是，它们仍然非常的不同。让我们来看一看。
       </p>
       <p>
        正如每一份Python程序员所知的那样，Python的类是开放的。你可以查看类里存放的全部内容，分离(detach)或是重连(reattach)类中的方法，即使类的声明已经结束。这一动态特性在解释器类中是不能使用的。为什么会这样呢？
       </p>
       <p>
        对于你为什么不能连接一个方法到字典类(比方说)这件事而言，其本身并没有什么技术上的限制。解释器不允许你这样做的原因实际上和程序员没什么关系，内建类型不在堆(heap)中.为了理解它的广泛影响，你需要明白Python是如何启动解释器的。
       </p>
       <p>
       </p>
       <h3>
        该死的解释器
       </h3>
       <p>
        在Python中，解释器的启动是一个消耗很大的过程。每当你启动一个可执行的Python文件，你调用了一个庞大的工具，做了全部的事情。此外，它会引导内部类型，它会启动导入的工具，它会导入需要的模块，和操作系统一起处理信号，接受命令行参数，启动内部状态等。当这些工作终于都完成之后，它会执行你的代码然后停止。Python这么做已经25年之久了。
       </p>
       <p>
        用伪代码展示上面过程，是这样的
       </p>
       <div class="crayon-syntax crayon-theme-github crayon-font-monaco crayon-os-mac print-yes notranslate" data-settings=" minimize scroll-always" id="crayon-55a64ab84c94e993877870">
        <div class="crayon-toolbar" data-settings=" show">
         <div class="crayon-tools">
          <div class="crayon-button crayon-nums-button crayon-pressed" title="切换是否显示行编号">
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
          </div>
          <p>
           <span class="crayon-language">
            Python
           </span>
          </p>
         </div>
        </div>
        <div class="crayon-plain-wrap">
        </div>
        <div class="crayon-main">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content">
              <div class="crayon-num" data-line="crayon-55a64ab84c94e993877870-1">
               1
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-55a64ab84c94e993877870-2">
               2
              </div>
              <div class="crayon-num" data-line="crayon-55a64ab84c94e993877870-3">
               3
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-55a64ab84c94e993877870-4">
               4
              </div>
              <div class="crayon-num" data-line="crayon-55a64ab84c94e993877870-5">
               5
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-55a64ab84c94e993877870-6">
               6
              </div>
              <div class="crayon-num" data-line="crayon-55a64ab84c94e993877870-7">
               7
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-55a64ab84c94e993877870-8">
               8
              </div>
              <div class="crayon-num" data-line="crayon-55a64ab84c94e993877870-9">
               9
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-55a64ab84c94e993877870-10">
               10
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre">
              <div class="crayon-line" id="crayon-55a64ab84c94e993877870-1">
               <span class="crayon-sy">
                ;
               </span>
               <span class="crayon-v">
                html
               </span>
               <span class="crayon-o">
                –
               </span>
               <span class="crayon-v">
                script
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-t">
                false
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-e">
                called
               </span>
               <span class="crayon-v">
                once
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-o">
                /
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c94e993877870-2">
               <span class="crayon-e">
                bootstrap
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c94e993877870-3">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c94e993877870-4">
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-e">
                these
               </span>
               <span class="crayon-e">
                three
               </span>
               <span class="crayon-e">
                could
               </span>
               <span class="crayon-e">
                be
               </span>
               <span class="crayon-e">
                called
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-i">
                a
               </span>
               <span class="crayon-e">
                loop
               </span>
               <span class="crayon-st">
                if
               </span>
               <span class="crayon-e">
                you
               </span>
               <span class="crayon-v">
                prefer
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-o">
                /
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c94e993877870-5">
               <span class="crayon-e">
                initialize
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c94e993877870-6">
               <span class="crayon-v">
                rv
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-e">
                run_code
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c94e993877870-7">
               <span class="crayon-e">
                finalize
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c94e993877870-8">
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c94e993877870-9">
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-e">
                called
               </span>
               <span class="crayon-v">
                once
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-o">
                /
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c94e993877870-10">
               <span class="crayon-e">
                shutdown
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        这样做的问题是，Python的解释器有一大堆的全局状态。实际上，你只能有一个解释器。一个更好的设计是启动解释器然后在上面运行一些东西：
       </p>
       <div class="crayon-syntax crayon-theme-github crayon-font-monaco crayon-os-mac print-yes notranslate" data-settings=" minimize scroll-always" id="crayon-55a64ab84c954121187962">
        <div class="crayon-toolbar" data-settings=" show">
         <div class="crayon-tools">
          <div class="crayon-button crayon-nums-button crayon-pressed" title="切换是否显示行编号">
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
          </div>
          <p>
           <span class="crayon-language">
            Python
           </span>
          </p>
         </div>
        </div>
        <div class="crayon-plain-wrap">
        </div>
        <div class="crayon-main">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content">
              <div class="crayon-num" data-line="crayon-55a64ab84c954121187962-1">
               1
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-55a64ab84c954121187962-2">
               2
              </div>
              <div class="crayon-num" data-line="crayon-55a64ab84c954121187962-3">
               3
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre">
              <div class="crayon-line" id="crayon-55a64ab84c954121187962-1">
               <span class="crayon-sy">
                ;
               </span>
               <span class="crayon-v">
                html
               </span>
               <span class="crayon-o">
                –
               </span>
               <span class="crayon-v">
                script
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-t">
                false
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-v">
                interpreter
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-v">
                iptr
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-e">
                make_interpreter
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                ;
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-55a64ab84c954121187962-2">
               <span class="crayon-e">
                interpreter_run_code
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                iptr
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-55a64ab84c954121187962-3">
               <span class="crayon-e">
                finalize_interpreter
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                iptr
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                ;
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <p>
        实际上这也是其他一些动态语言如何工作的。例如，这是lua的工作方式，javascript引擎的工作方式，等等。这样做明显的好处是你可以有两个解释器。多么新颖的概念啊。
       </p>
       <p>
        谁会需要多个解释器呢？你可能会感到惊讶。甚至是Python也需要，或是至少认为多个解释器是有用的。比如说，如果存在多个解释器，那么一个嵌入在Python中的应用程序可以单独的运行一些东西。（比如说，想象一些在mod_python中实现的web应用，它们想要独立的运行）。所以，在Python中，有一些子解释器。它们在解释器中工作，但是因为有太多的全局状态。全局状态中最大也是最容易引起冲突的一个是：全局解释器锁。Python已经选定了这种单一解释器的理念，所以子解释器之间有很多共享的数据。因为这些数据被共享，所以也就需要一个锁，所以这个所在实际的解释器中。哪些数据被共享了呢？
       </p>
       <p>
        如果你看一下我上面贴过的代码，你可以看到那些无所事事的结构体。实际上这些结构体是作为全局变量的。实际上，解释器把这些类型结构体直接暴露给Python代码。通过OB_HEAD_INIT(&amp;Typetype)宏来启动，这个宏给予结构体必要的数据头，以至于解释器可以用它工作。比如说，这里有一个该类型的引用计数(refcount)。
       </p>
       <p>
        现在你可以看到将要发生什么了。这些对象被子解释器所共享。所以，想象你可以在你的Python代码中改变这一对象。两个完全独立的Python代码，彼此间没有任何的关系，他们的状态都会改变。想象如果这是在JavaScript中，Faceboox选项卡将可以改变内建数组类型而且Google选项卡可以马上看到这一事件所带来的影响。
       </p>
       <p>
        这一1990年左右的设计方案，它带来的影响至今我们仍然可以感受的到。
       </p>
       <p>
        从好的方面来看,这种不可变的内建类型，已经越来越被社区当做一个好的特性所接受。可变的内建类型所带来的问题，已经被其他语言所证明，这不是我们所想要的。
       </p>
       <p>
        但仍然有很多是我们想要的。
       </p>
       <p>
       </p>
       <h3>
        什么是虚函数表(VTable)？
       </h3>
       <p>
        那些来自C语言的Python变量类型，大多是不可变的。那还有其他的什么东西是不同的呢？
       </p>
       <p>
        另外一个巨大的不同点仍然和Python类的开放特点有关。Python中的类有它们的“虚”方法。虽然这里并没有“真”的C++式的虚函数表，所有的方法都保存在类字典里而且有一种查询算法，但是归结起来其实几乎是一样的。这样做的结果很明显。当你创建一个子类并重载一个方法的时候，在这个过程中很有可能另外一个方法会间接的被改变，因为它进行了调用。
       </p>
       <p>
        另外一个好的例子是集合。很多集合都有很方便的方法。例如Python中的字典有两个方法来检索字典中的对象：
        <code>
         __getitem__()
        </code>
        和
        <code>
         get()
        </code>
        。当你在Python实现一个类的时候，你通常会通过诸如返回
        <code>
         self.__getitem__(key)
        </code>
        另一个类来实现它
       </p>
       <p>
        解释器对于类型的实现是不同的。其原因仍然是因为slots和字典间的不同。比如说你想要在解释器里实现一个字典。你的目标是重用代码，所以你要从
        <code>
         get
        </code>
        调用
        <code>
         __getitem__
        </code>
        。你要如何着手去做呢？
       </p>
       <p>
        C里面的Python方法，仅仅是一个具有特殊标识的C函数。这就是第一个问题了。这个函数首要目的是处理Python层的参数并把他们转换为你可以在C层使用的东西。至少，你需要从一个Python元组或字典(args and kwargs)中把单独的参数取出，放入局部变量。因此一个
        <br/>
        普通的模式是，
        <code>
         dict__getitem__
        </code>
        在内部仅仅解析参数，然后使用实际参数调用
        <code>
         dict_do_getitem
        </code>
        。你应该能料到接下来会怎样，
        <code>
         dict__getitem__
        </code>
        和
        <code>
         dict_get
        </code>
        都会调用
        <code>
         dict_get
        </code>
        ，它是一个内部静态函数。你不能够重载它。
       </p>
       <p>
        There really is no good way around this. The reason for this is related to the slot system. There is no good way from the interpreter internally issue a call through the vtable without going crazy. The reason for this is related to the global interpreter lock.when you are a dictionary your API contract to the outside world is that your operations are atomic. That contract completely goes out of the window when your internal call goes through a vtable. Why? Because that call might now go through Python code which needs to manage the global interpreter lock itself or you will run into massive problems.
        <br/>
        这里真的没有什么好办法了。这和槽(slots)系统有关。没有什么好办法让解释器正常的通过虚函数表来进行内部调用。
        <strong>
         这都是因为这个全局解释器锁。当作为一个字典的时候，字典通过API与外界联系。当内部调用遍历虚函数表的时候，这一联系就完全消失了。
        </strong>
        <br/>
        <strong>
         为什么？因为这一调用现在也需要通过Python代码，这需要它去处理全局解释器锁，不然就会产生大量的错误。
        </strong>
       </p>
       <p>
        Imagine the pain of a dictionary subclass overriding an internal dict_get which would kick off a lazy import. You throw all your guarantees out of the window. Then again, maybe we should have done that a long time ago.
        <br/>
        想象一个字典的子类重载一个内部
        <code>
         dict_get
        </code>
        的痛苦。你无法做出任何的保证。再说一次，也许我们很早以前就应该这样做了。
       </p>
       <p>
       </p>
       <h3>
        给将来的参考
       </h3>
       <p>
        近些年来，有一个明显的趋势就是要把Python变的更加复杂。我希望能够看到与之截然相反的趋势。
       </p>
       <p>
        我希望看到一个内部解释器的设计可以基于一些相互独立工作的解释器，拥有局部基类型，更像JavaScript的工作方式。这将马上打开基于消息传递的嵌入和并发的大门。CPU不会变的更快了:)
       </p>
       <p>
        与其把槽和字典看做是虚函数表，我们不如用字典做实验。Objective-C 作为一门语言，完全建立在消息机制上，这使得它的调用非常的快。它的调用，就我所知，比Python在最好的情况下，表现的还要快。字符串在Python里是被限制的，使得它的比较非常的快。我跟你赌，它一点都不慢，即使稍微慢那么一点点，它也会是一个更简单的系统，也就更容易优化。
       </p>
       <p>
        你应该看一下Python的代码库，处理槽(slots)统需要多少额外的逻辑呢？多到不可思议。
       </p>
       <p>
        我深信槽(slot)系统是个坏点子，在很久之前就该丢弃了。移除它甚至可能会对PyPy有所助益，因为我敢肯定他们需要故意限制解释器来使其像CPython一样，来获得兼容性。
       </p>
       <p>
        英文出处：
        <a href="http://lucumr.pocoo.org/2014/8/16/the-python-i-would-like-to-see/" target="_blank">
         Armin Ronacher
        </a>
       </p>
      </div>
      <div>
       <strong>
        注：转载文章均来自于公开网络，仅供学习使用，不会用于任何商业用途，如果侵犯到原作者的权益，请您与我们联系删除或者授权事宜，联系邮箱：contact@dataunion.org。转载数盟网站文章请注明原文章作者，否则产生的任何版权纠纷与数盟无关。
       </strong>
      </div>
      <!--content_text-->
      <div class="fenxian">
       <!-- JiaThis Button BEGIN -->
       <div class="jiathis_style_32x32">
        <p class="jiathis_button_weixin">
        </p>
        <p class="jiathis_button_tsina">
        </p>
        <p class="jiathis_button_qzone">
        </p>
        <p class="jiathis_button_cqq">
        </p>
        <p class="jiathis_button_tumblr">
        </p>
        <a class="jiathis jiathis_txt jtico jtico_jiathis" href="http://www.jiathis.com/share" target="_blank">
        </a>
        <p class="jiathis_counter_style">
        </p>
       </div>
       <!-- JiaThis Button END -->
      </div>
     </article>
     <!--content-->
     <!--相关文章-->
     <div class="xianguan">
      <div class="xianguantitle">
       相关文章！
      </div>
      <ul class="pic">
       <li>
        <a href="http://dataunion.org/20824.html">
         <img src="http://dataunion.org/wp-content/uploads/2015/09/t018630756a7e263b33-300x165.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/20824.html" rel="bookmark" title="如何判断一笔交易是否属于欺诈？你只是需要一点数据挖掘">
         如何判断一笔交易是否属于欺诈？你只是需要一点数据挖掘
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/20820.html">
         <img src="http://dataunion.org/wp-content/uploads/2015/09/1-300x200.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/20820.html" rel="bookmark" title="人们对Python在企业级开发中的10大误解">
         人们对Python在企业级开发中的10大误解
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/20811.html">
         <img src="http://dataunion.org/wp-content/uploads/2015/09/t0133fcacae8523307b_副本-300x200.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/20811.html" rel="bookmark" title="大神亲传：26条深度学习的金科玉律！">
         大神亲传：26条深度学习的金科玉律！
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/20808.html">
         <img src="http://dataunion.org/wp-content/uploads/2015/09/640.webp-11-300x137.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/20808.html" rel="bookmark" title="我们是如何在一张地图上表现86万个数据的">
         我们是如何在一张地图上表现86万个数据的
        </a>
       </li>
      </ul>
     </div>
     <!--相关文章-->
     <div class="comment" id="comments">
      <!-- You can start editing here. -->
      <!-- If comments are open, but there are no comments. -->
      <div class="title">
       期待你一针见血的评论，Come on！
      </div>
      <div id="respond">
       <p>
        不用想啦，马上
        <a href="http://dataunion.org/wp-login.php?redirect_to=http%3A%2F%2Fdataunion.org%2F20024.html">
         "登录"
        </a>
        发表自已的想法.
       </p>
      </div>
     </div>
     <!-- .nav-single -->
    </div>
    <!--Container End-->
    <aside id="sitebar">
     <div class="sitebar_list2">
      <div class="wptag">
       <span class="tagtitle">
        热门标签+
       </span>
       <div class="tagg">
        <ul class="menu" id="menu-%e5%8f%8b%e6%83%85%e9%93%be%e6%8e%a5">
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1605" id="menu-item-1605">
          <a href="http://taidizh.com/">
           泰迪智慧
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-20884" id="menu-item-20884">
          <a href="http://www.transwarp.cn/">
           星环科技
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-3538" id="menu-item-3538">
          <a href="http://datall.org/">
           珈和遥感
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-20888" id="menu-item-20888">
          <a href="http://www.chinahadoop.cn/">
           小象学院
          </a>
         </li>
        </ul>
       </div>
      </div>
     </div>
     <div class="sitebar_list">
      <div class="textwidget">
       <div align="center">
        <a href="http://study.163.com/course/courseMain.htm?courseId=991022" target="_blank">
         <img src="http://dataunion.org/wp-content/uploads/2016/03/dv.jpg"/>
        </a>
       </div>
      </div>
     </div>
     <div class="sitebar_list">
      <h4 class="sitebar_title">
       文章分类
      </h4>
      <div class="tagcloud">
       <a class="tag-link-44" href="http://dataunion.org/category/industry/demo" style="font-size: 10.204724409449pt;" title="4个话题">
        Demo展示
       </a>
       <a class="tag-link-31" href="http://dataunion.org/category/experts" style="font-size: 15.826771653543pt;" title="52个话题">
        专家团队
       </a>
       <a class="tag-link-870" href="http://dataunion.org/category/tech/ai" style="font-size: 19.795275590551pt;" title="273个话题">
        人工智能
       </a>
       <a class="tag-link-488" href="http://dataunion.org/category/%e5%8a%a0%e5%85%a5%e6%95%b0%e7%9b%9f" style="font-size: 8pt;" title="1个话题">
        加入数盟
       </a>
       <a class="tag-link-869" href="http://dataunion.org/category/tech/viz" style="font-size: 17.204724409449pt;" title="93个话题">
        可视化
       </a>
       <a class="tag-link-30" href="http://dataunion.org/category/partners" style="font-size: 10.645669291339pt;" title="5个话题">
        合作伙伴
       </a>
       <a class="tag-link-889" href="http://dataunion.org/category/parterc" style="font-size: 11.582677165354pt;" title="8个话题">
        合作会议
       </a>
       <a class="tag-link-104" href="http://dataunion.org/category/books" style="font-size: 12.96062992126pt;" title="15个话题">
        图书
       </a>
       <a class="tag-link-220" href="http://dataunion.org/category/tech/base" style="font-size: 19.850393700787pt;" title="281个话题">
        基础架构
       </a>
       <a class="tag-link-219" href="http://dataunion.org/category/tech/analysis" style="font-size: 19.409448818898pt;" title="232个话题">
        数据分析
       </a>
       <a class="tag-link-887" href="http://dataunion.org/category/tech/dm" style="font-size: 13.291338582677pt;" title="17个话题">
        数据挖掘
       </a>
       <a class="tag-link-34" href="http://dataunion.org/category/tech" style="font-size: 20.732283464567pt;" title="404个话题">
        文章
       </a>
       <a class="tag-link-1" href="http://dataunion.org/category/uncategorized" style="font-size: 22pt;" title="693个话题">
        未分类
       </a>
       <a class="tag-link-4" href="http://dataunion.org/category/events" style="font-size: 14.503937007874pt;" title="29个话题">
        活动
       </a>
       <a class="tag-link-890" href="http://dataunion.org/category/tech/%e6%b7%b1%e5%ba%a6%e5%ad%a6%e4%b9%a0" style="font-size: 10.204724409449pt;" title="4个话题">
        深度学习
       </a>
       <a class="tag-link-221" href="http://dataunion.org/category/tech/devl" style="font-size: 18.968503937008pt;" title="193个话题">
        编程语言
       </a>
       <a class="tag-link-888" href="http://dataunion.org/category/career" style="font-size: 15.661417322835pt;" title="48个话题">
        职业规划
       </a>
       <a class="tag-link-5" href="http://dataunion.org/category/jobs" style="font-size: 14.11811023622pt;" title="25个话题">
        职位
       </a>
       <a class="tag-link-871" href="http://dataunion.org/category/industry" style="font-size: 15.716535433071pt;" title="49个话题">
        行业
       </a>
       <a class="tag-link-613" href="http://dataunion.org/category/industry/case" style="font-size: 16.984251968504pt;" title="84个话题">
        行业应用
       </a>
       <a class="tag-link-885" href="http://dataunion.org/category/industry/news" style="font-size: 17.425196850394pt;" title="102个话题">
        行业资讯
       </a>
       <a class="tag-link-10" href="http://dataunion.org/category/training" style="font-size: 14.228346456693pt;" title="26个话题">
        课程
       </a>
       <a class="tag-link-16" href="http://dataunion.org/category/sources" style="font-size: 15.661417322835pt;" title="48个话题">
        资源
       </a>
      </div>
     </div>
     <div class="sitebar_list">
      <h4 class="sitebar_title">
       功能
      </h4>
      <ul>
       <li>
        <a href="http://dataunion.org/wp-login.php?action=register">
         注册
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/wp-login.php">
         登录
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/feed">
         文章
         <abbr title="Really Simple Syndication">
          RSS
         </abbr>
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/comments/feed">
         评论
         <abbr title="Really Simple Syndication">
          RSS
         </abbr>
        </a>
       </li>
       <li>
        <a href="https://cn.wordpress.org/" title="基于WordPress，一个优美、先进的个人信息发布平台。">
         WordPress.org
        </a>
       </li>
      </ul>
     </div>
    </aside>
    <div class="clear">
    </div>
   </div>
   <!--main-->
   ﻿
   <footer id="dibu">
    <div class="about">
     <div class="right">
      <ul class="menu" id="menu-%e5%ba%95%e9%83%a8%e8%8f%9c%e5%8d%95">
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-18024" id="menu-item-18024">
        <a href="http://dataunion.org/category/partners">
         合作伙伴
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-20881" id="menu-item-20881">
        <a href="http://dataunion.org/contribute">
         文章投稿
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20872" id="menu-item-20872">
        <a href="http://dataunion.org/category/%e5%8a%a0%e5%85%a5%e6%95%b0%e7%9b%9f">
         加入数盟
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-22441" id="menu-item-22441">
        <a href="http://dataunion.org/f-links">
         友情链接
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-20874" id="menu-item-20874">
        <a href="http://dataunion.org/aboutus">
         关于数盟
        </a>
       </li>
      </ul>
      <p class="banquan">
       数盟社区        ，
        做最棒的数据科学社区
      </p>
     </div>
     <div class="left">
      <ul class="bottomlist">
       <li>
        <a href="http://weibo.com/DataScientistUnion  " target="_blank" 　title="">
         <img src="http://dataunion.org/wp-content/themes/yzipi/images/weibo.png"/>
        </a>
       </li>
       <li>
        <a class="cd-popup-trigger" href="http://dataunion.org/20024.html#0">
         <img src="http://dataunion.org/wp-content/themes/yzipi/images/weixin.png"/>
        </a>
       </li>
      </ul>
      <div class="cd-popup">
       <div class="cd-popup-container">
        <h1>
         扫描二维码,加微信公众号
        </h1>
        <img src="http://dataunion.org/wp-content/themes/yzipi/images/2014-12-06-1515289049.png"/>
        <a class="cd-popup-close" href="http://dataunion.org/20024.html">
        </a>
       </div>
       <!-- cd-popup-container -->
      </div>
      <!-- cd-popup -->
     </div>
    </div>
    <!--about-->
    <div class="bottom">
     <a href="http://dataunion.org/">
      数盟社区
     </a>
     <a href="http://www.miitbeian.gov.cn/" rel="external nofollow" target="_blank">
      京ICP备14026740号
     </a>
     联系我们：
     <a href="mailto:contact@dataunion.org" target="_blank">
      contact@dataunion.org
     </a>
     <div class="tongji">
     </div>
     <!--bottom-->
     <div class="scroll" id="scroll" style="display:none;">
      ︿
     </div>
    </div>
   </footer>
   <!--dibu-->
  </div>
 </body>
</html>